<template>
  <view class="price-modal-overlay" v-if="visible" @click="handleOverlayClick">
    <view class="price-modal" @click.stop>
      <!-- 弹窗头部 -->
      <view class="modal-header">
        <text class="modal-title">设置售价</text>
        <view class="close-btn" @click="closeModal">
          <text class="close-icon">×</text>
        </view>
      </view>
      
      <!-- 弹窗内容 -->
      <view class="modal-content">
        <view class="price-input-section">
          <text class="input-label">卡密售价</text>
          <view class="input-container">
            <text class="currency-symbol">¥</text>
            <input 
              class="price-input" 
              type="digit"
              placeholder="请输入售价"
              v-model="inputPrice"
              @input="handleInput"
              :focus="inputFocus"
            />
          </view>
          <text class="input-hint">请输入合理的售价，建议参考市场价格</text>
        </view>
        
        <!-- 快捷价格选项 -->
        <view class="quick-price-section">
          <text class="section-label">快捷设置</text>
          <view class="quick-price-buttons">
            <view 
              class="quick-price-btn"
              v-for="(price, index) in quickPrices" 
              :key="index"
              @click="selectQuickPrice(price)"
            >
              <text class="quick-price-text">¥{{ price }}</text>
            </view>
          </view>
        </view>
      </view>
      
      <!-- 弹窗底部按钮 -->
      <view class="modal-footer">
        <view class="footer-btn cancel-btn" @click="closeModal">
          <text class="btn-text cancel-text">取消</text>
        </view>
        <view class="footer-btn confirm-btn" :class="{ disabled: !isValidPrice }" @click="confirmPrice">
          <text class="btn-text confirm-text">确定</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: 'PriceModal',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    defaultPrice: {
      type: [String, Number],
      default: ''
    },
    quickPrices: {
      type: Array,
      default: () => ['9.9', '19.9', '29.9', '49.9', '99.9']
    }
  },
  data() {
    return {
      inputPrice: '',
      inputFocus: false
    }
  },
  computed: {
    isValidPrice() {
      const price = parseFloat(this.inputPrice);
      return !isNaN(price) && price > 0;
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        this.inputPrice = this.defaultPrice ? String(this.defaultPrice) : '';
        // 延迟聚焦，确保弹窗完全显示后再聚焦
        setTimeout(() => {
          this.inputFocus = true;
        }, 300);
      } else {
        this.inputFocus = false;
      }
    }
  },
  methods: {
    handleOverlayClick() {
      this.closeModal();
    },
    
    closeModal() {
      this.$emit('close');
    },
    
    handleInput(e) {
      let value = e.detail.value;
      // 只允许数字和小数点
      value = value.replace(/[^\d.]/g, '');
      // 确保只有一个小数点
      const parts = value.split('.');
      if (parts.length > 2) {
        value = parts[0] + '.' + parts.slice(1).join('');
      }
      // 限制小数点后最多两位
      if (parts[1] && parts[1].length > 2) {
        value = parts[0] + '.' + parts[1].substring(0, 2);
      }
      this.inputPrice = value;
    },
    
    selectQuickPrice(price) {
      this.inputPrice = String(price);
    },
    
    confirmPrice() {
      if (!this.isValidPrice) {
        uni.showToast({
          title: '请输入有效的售价',
          icon: 'none'
        });
        return;
      }
      
      const price = parseFloat(this.inputPrice);
      this.$emit('confirm', price);
    }
  }
}
</script>

<style scoped>
.price-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 16px;
  backdrop-filter: blur(4px);
  animation: overlayFadeIn 0.3s ease-out;
}

/* 移动端适配 */
@media (max-width: 480px) {
  .price-modal-overlay {
    padding: 12px;
    align-items: flex-end;
  }
}

@keyframes overlayFadeIn {
  from {
    opacity: 0;
    backdrop-filter: blur(0px);
  }
  to {
    opacity: 1;
    backdrop-filter: blur(4px);
  }
}

.price-modal {
  width: 100%;
  max-width: 360px;
  background-color: #ffffff;
  border-radius: 20px;
  overflow: hidden;
  box-shadow: 0 12px 48px rgba(0, 0, 0, 0.2);
  animation: modalSlideIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* 移动端适配 */
@media (max-width: 480px) {
  .price-modal {
    max-width: 100%;
    border-radius: 20px 20px 0 0;
    animation: modalSlideUp 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  }
}

@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: scale(0.8) translateY(40px);
  }
  to {
    opacity: 1;
    transform: scale(1) translateY(0);
  }
}

/* 移动端从底部滑入动画 */
@keyframes modalSlideUp {
  from {
    opacity: 0;
    transform: translateY(100%);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* 弹窗头部 */
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 20px 16px;
  border-bottom: 1px solid #f0f0f0;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  color: #333333;
}

.close-btn {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
  background-color: #f5f5f5;
  transition: background-color 0.2s;
}

.close-btn:active {
  background-color: #e8e8e8;
}

.close-icon {
  font-size: 20px;
  color: #666666;
  line-height: 1;
}

/* 弹窗内容 */
.modal-content {
  padding: 24px 20px;
}

.price-input-section {
  margin-bottom: 24px;
}

.input-label {
  font-size: 16px;
  font-weight: 500;
  color: #333333;
  margin-bottom: 12px;
  display: block;
}

.input-container {
  position: relative;
  display: flex;
  align-items: center;
  background-color: #f8f9fa;
  border: 2px solid #e9ecef;
  border-radius: 16px;
  padding: 0 20px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

.input-container:focus-within {
  border-color: #ff4757;
  background-color: #fff;
  box-shadow: 0 0 0 4px rgba(255, 71, 87, 0.12), 0 4px 16px rgba(0, 0, 0, 0.08);
  transform: translateY(-1px);
}

.currency-symbol {
  font-size: 18px;
  font-weight: 600;
  color: #ff4757;
  margin-right: 8px;
}

.price-input {
  flex: 1;
  height: 56px;
  font-size: 20px;
  font-weight: 600;
  color: #333333;
  background: transparent;
  border: none;
  outline: none;
  letter-spacing: 0.5px;
}

.price-input::placeholder {
  color: #999999;
  font-weight: 400;
  letter-spacing: normal;
}

.input-hint {
  font-size: 12px;
  color: #999999;
  margin-top: 8px;
  display: block;
}

/* 快捷价格选项 */
.quick-price-section {
  margin-bottom: 8px;
}

.section-label {
  font-size: 14px;
  font-weight: 500;
  color: #666666;
  margin-bottom: 12px;
  display: block;
}

.quick-price-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: space-between;
}

/* 移动端快捷按钮适配 */
@media (max-width: 480px) {
  .quick-price-buttons {
    gap: 6px;
  }
  
  .quick-price-btn {
    flex: 1;
    min-width: 0;
    text-align: center;
  }
  
  .quick-price-text {
    font-size: 13px;
  }
}

.quick-price-btn {
  padding: 10px 18px;
  background-color: #f8f9fa;
  border: 2px solid #e9ecef;
  border-radius: 24px;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
}

.quick-price-btn:active {
  background: linear-gradient(135deg, #ff4757 0%, #ff3742 100%);
  border-color: #ff4757;
  transform: scale(0.96) translateY(1px);
  box-shadow: 0 4px 12px rgba(255, 71, 87, 0.3);
}

.quick-price-btn:active .quick-price-text {
  color: #ffffff;
  font-weight: 600;
}

.quick-price-text {
  font-size: 14px;
  color: #666666;
  font-weight: 500;
}

/* 弹窗底部 */
.modal-footer {
  display: flex;
  border-top: 1px solid #f0f0f0;
}

.footer-btn {
  flex: 1;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.cancel-btn {
  background-color: #f8f9fa;
  border-right: 1px solid #f0f0f0;
}

.cancel-btn:active {
  background-color: #e9ecef;
}

.confirm-btn {
  background: linear-gradient(135deg, #ff4757 0%, #ff3742 100%);
  position: relative;
  overflow: hidden;
}

.confirm-btn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}

.confirm-btn:active {
  background: linear-gradient(135deg, #ff3742 0%, #ff2a36 100%);
  transform: translateY(1px);
}

.confirm-btn:active::before {
  left: 100%;
}

.confirm-btn.disabled {
  background-color: #cccccc;
  pointer-events: none;
}

.btn-text {
  font-size: 16px;
  font-weight: 500;
}

.cancel-text {
  color: #666666;
}

.confirm-text {
  color: #ffffff;
}

.confirm-btn.disabled .confirm-text {
  color: #999999;
}
</style>